const express = require("express");

const mysql = require("mysql");

const app = express();

app.use(function(req,res,next){
  // next与路由里面的next全局钩子函数一样，如果满足就走到下面去，如果不想往下走就不写
  console.log("这个肯定进来");

  // 处理跨域，这个是允许的请求的地址的源
  res.set('Access-Control-Allow-Origin','*');


  res.set("Access-Control-Allow-Headers","content-type");//如果是post必须设置这一行。它是允许的

  // 请求的自定义信息头

  //post执行时先回执行一个opotions的跨域，然后才执行post的跨域
  next();
})

// app.get('/login',function(req,res){
//   // 数据库连接
//   var connection = mysql.createConnection({
//     host : "localhost",
//     user : "root",
//     password : "123",
//     database : "eleme"
//   });
//   // 为了代理前端不用写代理，直接在后端里面设置cors
//   // use是中间键

//   connection.connect();
//   var username = req.query.username;
//   var password = req.query.password;
//   console.log(username,password);
//   var sql = `select * from t_user where uname = '${username}'  and password = '${password}'`;
//   connection.query(sql,function(err,result,fields){
//     if(err){
//       console.log(err);
//       res.send({code:0,msg:"登录失败"});
//     }else{
//       console.log(result);
//       console.log(fields);
//       res.send({code:1,msg:"登录成功  "});
//     }
//     connection.end();
//   })
// })

app.post('/login',function(req,res){
  // 数据库连接
  var connection = mysql.createConnection({
    host :'10.36.134.112',
    user : "root",
    password : "123",
    database : "eleme"
  });
  // 为了代理前端不用写代理，直接在后端里面设置cors
  // use是中间键

  connection.connect();
  var body = "";
  req.on('data',function(chunk){
    body+=chunk;
  })
  req.on('end',function(){
    body = JSON.parse(body);
    console.log(body);
    var username = body.params.username;
    var password = body.params.password;
    console.log(username,password);
    var sql = `select * from t_user where uname = '${username}'  and password = '${password}'`;
    console.log('sql语句');
    connection.query(sql,function(err,result,fields){
      console.log('进入查询');
      if(err){
        console.log('错误');
        console.log(err);
        res.send({code:0,msg:"登录失败"});
      }else{
        console.log('成功');
        console.log(result);
        console.log(fields);

        if(result.length==1){
          res.send({code:1,msg:"登录成功"});
        }else{
          res.send({code:0,msg:"登录失败"});
        }

      }
      connection.end();
  })

  })
})
app.listen(3000);

app.post('/register',function(req,res){
  // 数据库连接
  var connection = mysql.createConnection({
    host : "10.36.134.112",
    user : "root",
    password : "123",
    database : "eleme"
  });
  // 为了代理前端不用写代理，直接在后端里面设置cors
  // use是中间键

  connection.connect();
  var body = "";
  req.on('data',function(chunk){
    body+=chunk;
  })
  req.on('end',function(){
    body = JSON.parse(body);
    console.log(body);
    var username = body.params.username;
    var password = body.params.password;
    console.log(username,password);
    var sql = `insert into t_user (uname,password) values('username','password')`;
    connection.query(sql,function(err,result,fields){
      if(err){
        console.log(err);
        res.send({code:0,msg:"注册失败"});
      }else{
        res.send({code:1,msg:"注册成功  "});
      }
      connection.end();
  })

  })
})


